//
//  TaskDiaoDuOrderGoodVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2021/4/2.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "TaskDiaoDuOrderGoodVC.h"
#import "TaskDiaoDuOrderGoodCell.h"

@interface TaskDiaoDuOrderGoodVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation TaskDiaoDuOrderGoodVC

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"货物详情";
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
    UIButton *editBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 88, 30)];
    [editBtn setTitle:@"确认" forState:0];
    [editBtn setTitleColor:HexColor(0xffffff) forState:0];
    editBtn.titleLabel.font = BoldFontOfSize(14);
    editBtn.backgroundColor = Color_ThemeColor;
    editBtn.layer.masksToBounds = YES;
    editBtn.layer.cornerRadius = 15;
    [editBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:editBtn];
}

- (void)sureClick {
    __block BOOL canPop = YES;
    [self.goods_list enumerateObjectsUsingBlock:^(TaskDiaoDuOrderGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.select_pnum <= 0) {
            canPop = NO;
            [MBProgressHUD leShowFailedHUD:self.view message:@"装载件数必须大于0"];
            return;
        }
        if (obj.select_num <= 0) {
            canPop = NO;
            [MBProgressHUD leShowFailedHUD:self.view message:@"装载数量必须大于0"];
            return;
        }
        if (obj.select_weight == 0 && obj.select_volume == 0) {
            canPop = NO;
            [MBProgressHUD leShowFailedHUD:self.view message:@"装载重量和体积必填一项"];
            return;
        }
    }];
    if (canPop) {
        if (self.finishBlock) {
            self.finishBlock(self.goods_list);
        }
        [self.rt_navigationController popViewControllerAnimated:YES];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.goods_list.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TaskDiaoDuOrderGoodCell *cell = [TaskDiaoDuOrderGoodCell TaskDiaoDuOrderGoodCellWithTableView:tableView];
    TaskDiaoDuOrderGoodModel *model = self.goods_list[indexPath.section];
    cell.model = model;
    __weak_self;
    cell.pnumBlock = ^(NSString * _Nonnull pnum) {
        if ([pnum intValue] > model.residue_pnum) {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"装载件数不能超过剩余件数"];
            model.select_pnum = 0;
            [tableView reloadData];
        }else {
            model.select_pnum = [pnum intValue];
        }
    };
    cell.countBlock = ^(NSString * _Nonnull count) {
        if ([count intValue] > model.residue_num) {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"装载数量不能超过剩余数量"];
            model.select_num = 0;
            [tableView reloadData];
        }else {
            model.select_num = [count intValue];
        }
    };
    cell.weightBlock = ^(NSString * _Nonnull weight) {
        if ([weight floatValue] > model.residue_weight) {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"装载重量不能超过剩余重量"];
            model.select_weight = 0.0;
            [tableView reloadData];
        }else {
            model.select_weight = [weight floatValue];
        }
    };
    cell.volumeBlock = ^(NSString * _Nonnull volume) {
        if ([volume floatValue] > model.residue_volume) {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"装载体积不能超过剩余体积"];
            model.select_volume = 0.0;
            [tableView reloadData];
        }else {
            model.select_volume = [volume floatValue];
        }
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    TaskDiaoDuOrderGoodCell *cell = [TaskDiaoDuOrderGoodCell TaskDiaoDuOrderGoodCellWithTableView:tableView];
    TaskDiaoDuOrderGoodModel *model = self.goods_list[indexPath.section];
    cell.model = model;
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
//    view.backgroundColor = Color_backgroundColor;
//    return view;
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
